<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

$title = $txt['bnt_pr_title'];

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Get all GET variables
$PRepType = (isset($_GET['PRepType'])) ? $_GET['PRepType'] : '';
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : '';

// Display the menu if no valid options are passed in.
if (empty($PRepType))
{
	$title .= ': Status';
	BigTitle($title);
	standard_report($playerinfo);
}
// Display the commodities on the planets.
if ($PRepType == 1) 
{
	$title .= ': Status';
	BigTitle($title);
	standard_report($playerinfo);
}
// Display the production values of your planets and allow changing.
elseif ($PRepType == 2)
{
	$title .= ': Production';
	BigTitle($title);
	planet_production_change($playerinfo,$sort);
}
// Planetary defenses display
elseif ($PRepType == 4)
{
	$title .= ': ' . $txt['bnt_pr_defenses'];
	BigTitle($title);
	planet_defenses($playerinfo);
}


// Begin functions.
function standard_report($playerinfo)
{
	global $smcFunc, $txt, $scripturl;
	global $dbtables;

	// Pull all player owned planets
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE owner = {int:ship_id}
		ORDER BY sector_id ASC',
		array(
			'db_planets' => $dbtables['planets'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	$i = 0;
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		$planet[$i] = $row;
		$i++;
	}
	$smcFunc['db_free_result']($result);
	
	$num_planets = $i;
	
	if ($num_planets < 1)
		echo '<br>', $txt['bnt_pr_noplanet'];
	else
	{
		echo '
		<script type="text/javascript">
			$(document).ready(function(){
				// Table sorting function
				$("#planet_resource_table").tablesorter({
					headers: { 
						0: {sorter: false},
						3: { 
							sorter: "fancyNumber" 
						},
						4: { 
							sorter: "fancyNumber" 
						},
						5: { 
							sorter: "fancyNumber" 
						},
						6: { 
							sorter: "fancyNumber" 
						},
						7: { 
							sorter: "fancyNumber" 
						},
						8: { 
							sorter: "fancyNumber" 
						}
					},
					widgets: ["zebra","rowHover"]
				});
			});
		</script>
		<input id="current_sector" type="hidden" value="', $playerinfo['sector'], '" />
		<div id="planet_report_button"><span id="collect_credits_engage" class="bnt_button ui-state-default ui-corner-all">', $txt['bnt_pr_colcredits'], '</span></div>';
		
		// Next block of echo's creates the header of the table.
		echo '
	    <table id="planet_resource_table" class="bnt_table">
			<thead>
				<tr>
					<th><input type="checkbox" class="check" id="planet_checkall" /></th>
					<th>', $txt['bnt_sector'], '</th>
					<th>', $txt['bnt_name'], '</th>
					<th>', $txt['bnt_ore'], '</th>
					<th>', $txt['bnt_organics'], '</th>
					<th>', $txt['bnt_goods'], '</th>
					<th>', $txt['bnt_energy'], '</th>
					<th>', $txt['bnt_colonists'], '</th>
					<th>', $txt['bnt_credits'], '</th>
				</tr>
			</thead>
			<tbody>';
		
	    $total_organics = 0;
	    $total_ore = 0;
	    $total_goods = 0;
	    $total_energy = 0;
	    $total_colonists = 0;
	    $total_credits = 0;
	    $total_base = 0;
	    $total_corp = 0;
	    $total_selling = 0;
		
		for ($i = 0; $i < $num_planets; $i++)
		{
			$total_organics += $planet[$i]['organics'];
			$total_ore += $planet[$i]['ore'];
			$total_goods += $planet[$i]['goods'];
			$total_energy += $planet[$i]['energy'];
			$total_colonists += $planet[$i]['colonists'];
			$total_credits += $planet[$i]['credits'];
			
			if ($planet[$i]['corp'] > 0)
				$total_corp += 1;
			if (empty($planet[$i]['name']))
				$planet[$i]['name'] = $txt['bnt_unnamed'];
				
			echo '
				<tr id="', $planet[$i]['planet_id'] ,'">
					<td><input type="checkbox" style="check" name="credits_planet_id" value="', $planet[$i]['planet_id'], '"\></td>
					<td><a class="rs_link" href="#">', $planet[$i]['sector_id'], '</a></td>
					<td>
						<ul>
						<li><span class="planet_rename" title="', $txt['bnt_pr_rename'], '">', $planet[$i]['name'], '</span></li>
						<li>
							<span>', base_build_check($planet, $i), '</span>
							<span>';
			
			if ($planet[$i]['base'] == 'Y')
				echo '<span class="planet_sells" title="Toggle Selling"><input type="hidden" value="', $planet[$i]['planet_id'] ,'">', $planet[$i]['sells'] == 'Y' ? t_port_icon("sells") : t_port_icon("sells_none"), '</span>';
				
			echo '</span>
						</li>
						</ul>
					</td>
					<td>', NUMBER($planet[$i]['ore']), '</td>
					<td>', NUMBER($planet[$i]['organics']), '</td>
					<td>', NUMBER($planet[$i]['goods']), '</td>
					<td>', NUMBER($planet[$i]['energy']), '</td>
					<td>', NUMBER($planet[$i]['colonists']), '</td>
					<td>', NUMBER($planet[$i]['credits']), '</td>
				</tr>';
		}

		// The next block displays the totals.
		echo '
			</tbody>
			<tfoot>
				<tr>
					<td colspan="3">', $txt['bnt_pr_totals'], '</td>
					<td>', NUMBER($total_ore), '</td>
					<td>', NUMBER($total_organics), '</td>
					<td>', NUMBER($total_goods), '</td>
					<td>', NUMBER($total_energy), '</td>
					<td>', NUMBER($total_colonists), '</td>
					<td>', NUMBER($total_credits), '</td>
				</tr>
			</tfoot>
		</table>';
	}
}

function planet_defenses($playerinfo)
{
	global $smcFunc, $txt, $scripturl;
	global $dbtables;
	
	// Setup javascript table sorter function
	echo '
					<script type="text/javascript">
						$(document).ready(function(){
							// Table sorting function
							$("#planet_defence_table").tablesorter({
								headers: { 
									2: { 
										sorter: "fancyNumber" 
									},
									3: { 
										sorter: "fancyNumber" 
									},
									4: { 
										sorter: "fancyNumber" 
									}
								},
								widgets: ["zebra","rowHover"]
							});
						});
					</script>
					<input id="current_sector" type="hidden" value="', $playerinfo['sector'], '" />';
	
	// Pull all player owned planets
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE owner = {int:ship_id}
		ORDER BY sector_id ASC',
		array(
			'db_planets' => $dbtables['planets'],
			'ship_id' => $playerinfo['ship_id']
		)
	);

	$i = 0;
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		$planet[$i] = $row;
		$i++;
	}
	$smcFunc['db_free_result']($result);
	
	$num_planets = $i;
	if ($num_planets < 1)
		echo '<br>', $txt['bnt_pr_noplanet'];
	else
	{
	
	echo '
	
					<table id="planet_defence_table" class="bnt_table">
						<thead>
							<tr>
								<th>', $txt['bnt_sector'], '</th>
								<th>', $txt['bnt_name'], '</th>
								<th>', $txt['bnt_energy'], '</th>
								<th>', $txt['bnt_fighters'], '</th>
								<th>', $txt['bnt_torps'], '</th>
							</tr>
						</thead>
						<tbody>';
	
	$total_energy = 0;
	$total_credits = 0;
	$total_fighters = 0;
	$total_torp = 0;
	
	// Parse through all the planets and display the results
	for ($i = 0; $i < $num_planets; $i++)
	{
		$total_energy += $planet[$i]['energy'];
		$total_fighters += $planet[$i]['fighters'];
		$total_torp += $planet[$i]['torps'];
			
		if (empty($planet[$i]['name']))
			$planet[$i]['name'] = $txt['bnt_unnamed'];
				
		echo '
							<tr id="', $planet[$i]['planet_id'] ,'">
								<td><a class="rs_link" href="#">', $planet[$i]['sector_id'], '</a></td>
								<td><ul><li><span class="planet_rename" title="', $txt['bnt_pr_rename'], '">', $planet[$i]['name'], '</span></li></ul></td>
								<td>', NUMBER($planet[$i]['energy']), '</td>
								<td>', NUMBER($planet[$i]['fighters']), '</td>
								<td>', NUMBER($planet[$i]['torps']), '</td>
							</tr>';
		}

		// The next block displays the totals.
		echo '
						</tbody>
						<tfoot>
							<tr>
								<td colspan="2">', $txt['bnt_pr_totals'], '</td>
								<td>', NUMBER($total_energy), '</td>
								<td>', NUMBER($total_fighters), '</td>
								<td>', NUMBER($total_torp), '</td>
							</tr>
						</tfoot>
					</table>';
	}
}

function planet_production_change($playerinfo,$sort)
{
	global $smcFunc, $txt, $scripturl;
	global $dbtables;

	// Pull all player owned planets
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE owner = {int:ship_id}
		AND base = "Y"
		ORDER BY sector_id ASC',
		array(
			'db_planets' => $dbtables['planets'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	$i = 0;
	while($row = $smcFunc['db_fetch_assoc']($result))
	{
		$planet[$i] = $row;
		$i++;
	}
	$smcFunc['db_free_result']($result);
	
	
	$num_planets = $i;
	if ($num_planets < 1)
		echo '<p>', $txt['bnt_pr_nobases'], '</p>';
	else
	{
		echo '
		<input id="current_sector" type="hidden" value="', $playerinfo['sector'], '" />';

		// Next block of echo's creates the header of the table.
		echo '
		<script type="text/javascript">
			$(document).ready(function(){
				// Table sorting function
				$("#planet_production_table").tablesorter({
					headers: { 
						0: {sorter: false},
						3: {sorter: false},
						4: {sorter: false},
						5: {sorter: false},
						6: {sorter: false},
						7: {sorter: false},
						8: {sorter: false},
						9: {sorter: false}
					},
					widgets: ["zebra"]
				});
				$("#global_production_table").tablesorter({
					headers: { 
						0: {sorter: false}
					},
					widgets: ["zebra"]
				});
				// Check for planets with global production enabled
				$(".global_toggle").each( function () {
					if ($(this).attr("checked"))
					{
						var planet_id = $(this).attr("name");
						$("." + planet_id).attr("disabled", true);
						$("." + planet_id).addClass("bnt_disabled");
					}
				});
				// Global production check all checkbox
				$("#global_production_checkall").toggle(
					function () {
						$(":checkbox").attr("checked", true);
						$("input[class^=\'global_toggle_\']").attr("disabled", true);
						$("input[class^=\'global_toggle_\']").addClass("bnt_disabled");
					},
					function () {
						$(":checkbox").removeAttr("checked");
						$("input[class^=\'global_toggle_\']").removeAttr("disabled");
						$("input[class^=\'global_toggle_\']").removeClass("bnt_disabled");
				});
				// OnChange updates for production values 
				$("input:text").change( function() {
					$.update_row = {
						planet_id: $(this).closest("tr").attr("id"),
						credits: 0
					}
					$(".global_toggle_" + $.update_row.planet_id).each(function() {
						$.update_row.credits = $.update_row.credits + ($(this).val() - 0);
					});
					$("#credits_" + $.update_row.planet_id).html((100 - $.update_row.credits) + "%");
				});
			});
		</script>
		<div id="planet_report_button"><span id="change_production" class="bnt_button ui-state-default ui-corner-all">', $txt['bnt_pr_change'], '</span></div>
		<table id="global_production_table" class="bnt_table">
			<thead>
				<tr>
					<th colspan="7" align="center">', $txt['bnt_pr_globalsettings'], '</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>', $txt['bnt_ore'], '</td>
					<td>', $txt['bnt_organics'], '</td>
					<td>', $txt['bnt_goods'], '</td>
					<td>', $txt['bnt_energy'], '</td>
					<td>', $txt['bnt_fighters'], '</td>
					<td>', $txt['bnt_torps'], '</td>
					<td>', $txt['bnt_credits'], '</td>
				</tr>
				<tr id="global">
					<td><input type="text" id="global_setting_ore" name="global_ore" value="', $playerinfo['global_ore'], '">%</td>
					<td><input type="text" id="global_setting_organics" name="global_organics" value="', $playerinfo['global_organics'], '">%</td>
					<td><input type="text" id="global_setting_goods" name="global_goods" value="', $playerinfo['global_goods'], '">%</td>
					<td><input type="text" id="global_setting_energy" name="global_energy" value="', $playerinfo['global_energy'], '">%</td>
					<td><input type="text" id="global_setting_fighters" name="global_fighters" value="', $playerinfo['global_fighters'], '">%</td>
					<td><input type="text" id="global_setting_torps" name="global_torps" value="', $playerinfo['global_torps'], '">%</td>';
		
		$global_credits = 100 - ($playerinfo['global_ore'] + $playerinfo['global_organics'] + $playerinfo['global_goods'] + $playerinfo['global_energy'] + $playerinfo['global_fighters'] + $playerinfo['global_torps']);

		echo '
					<td id="credits_global">', $global_credits ,'%</td>
				</tr>
			</tbody>
		</table>
		<table id="planet_production_table" class="bnt_table">
			<thead>
				<tr>
					<th id="global_production_checkall">', $txt['bnt_pr_global'], '</th>
					<th>', $txt['bnt_sector'], '</th>
					<th>', $txt['bnt_name'], '</th>
					<th>', $txt['bnt_ore'], '</th>
					<th>', $txt['bnt_organics'], '</th>
					<th>', $txt['bnt_goods'], '</th>
					<th>', $txt['bnt_energy'], '</th>
					<th>', $txt['bnt_fighters'], '</th>
					<th>', $txt['bnt_torps'], '</th>
					<th>', $txt['bnt_credits'], '</th>
				</tr>
			</thead>
			<tbody>';

		$total_colonists = 0;
		$total_credits = 0;
		$total_corp = 0;

		$temp_var = 0;

		for ($i = 0; $i < $num_planets; $i++)
		{
			$total_colonists += $planet[$i]['colonists'];
			$total_credits += $planet[$i]['credits'];
			if (empty($planet[$i]['name']))
				$planet[$i]['name'] = $txt['bnt_unnamed'];

			echo '
				<tr id="', $planet[$i]['planet_id'], '">
					<td><input type="checkbox" style="check" class="global_toggle" name="global_toggle_', $planet[$i]['planet_id'], '" ';
			
			if ($planet[$i]['prod_global'] == 'Y')
				echo 'checked="yes"';

			echo ' /></td>
					<td><a class="rs_link" href="#">', $planet[$i]['sector_id'], '</a></td>
					<td><ul><li><span class="planet_rename" title="', $txt['bnt_pr_rename'], '">', $planet[$i]['name'], '</span></li></ul></td>
					<td><input type="text" class="global_toggle_', $planet[$i]['planet_id'], '" name="prod_ore" maxlength="3" value="', $planet[$i]['prod_ore'], '" />%</td>
					<td><input type="text" class="global_toggle_', $planet[$i]['planet_id'], '" name="prod_organics" maxlength="3" value="', $planet[$i]['prod_organics'], '" />%</td>
					<td><input type="text" class="global_toggle_', $planet[$i]['planet_id'], '" name="prod_goods" maxlength="3" value="', $planet[$i]['prod_goods'], '" />%</td>
					<td><input type="text" class="global_toggle_', $planet[$i]['planet_id'], '" name="prod_energy" maxlength="3" value="', $planet[$i]['prod_energy'], '" />%</td>
					<td><input type="text" class="global_toggle_', $planet[$i]['planet_id'], '" name="prod_fighters" maxlength="3" value="', $planet[$i]['prod_fighters'], '" />%</td>
					<td><input type="text" class="global_toggle_', $planet[$i]['planet_id'], '" name="prod_torp" maxlength="3" value="', $planet[$i]['prod_torp'], '" />%</td>';
			
			$planet_credits = 100 - ($planet[$i]['prod_ore'] + $planet[$i]['prod_organics'] + $planet[$i]['prod_goods'] + $planet[$i]['prod_energy'] + $planet[$i]['prod_fighters'] + $planet[$i]['prod_torp']);
			
			echo '
					<td id="credits_', $planet[$i]['planet_id'], '">', $planet_credits, '%</td>
				</tr>';
		}
		
		echo '
			</tbody>
		</table>';
	}
}

function corp_planet_checkboxes($planet, $i)
{
	if ($planet[$i]['corp'] <= 0)
		return("<input type=checkbox style=check name=corp[] value=\"" . $planet[$i]['planet_id'] . "\">");
	elseif ($planet[$i]['corp'] > 0)
		return("<input type=checkbox style=check name=corp[] value=\"" . $planet[$i]['planet_id'] . "\" checked>");
}

function base_build_check($planet, $i)
{
	global $txt, $scripturl;
	global $base_ore, $base_organics, $base_goods, $base_credits;

	if ($planet[$i]['base'] == 'Y')
		return(t_port_icon("base"));
	elseif ($planet[$i]['ore'] >= $base_ore && $planet[$i]['organics'] >= $base_organics && $planet[$i]['goods'] >= $base_goods && $planet[$i]['credits'] >= $base_credits)
		return('<span id="build_base" title="Build Base"><input id="buildp" type="hidden" value="' . $planet[$i]['planet_id'] . '" /><input id="builds" type="hidden" value="' . $planet[$i]['sector_id'] . '" />' . t_port_icon("base_none") . '</span>');
}

?>
